package com.zhentao.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result {

    public static String SUCCESS = "200";//成功
    public static String FAILED = "1";//业务操作失败
    public static String LOGIN_EXPIRE = "0";//登录已超期

    public static String SERVER_INTERNAL_ERROR = "500";//服务器内部错误
    public static String NOT_FOUND = "404";//访问资源不存在
    public static String NO_AUTH = "401";//无权访问资源


    /**
     * 结果码
     */
    private String code = SUCCESS; //默认成功

    /**
     * 操作结果
     * true 成功，false 失败
     */
    private boolean result = true; //默认成功

    /**
     * 信息
     */
    private String msg;

    /**
     * 返回给客户端的数据
     */
    private Object data;

    public Result(){
    }

    public Result(String code, boolean result) {
        this.code = code;
        this.result = result;
    }

    public Result(String code, boolean result, String msg) {
        this.code = code;
        this.result = result;
        this.msg = msg;
    }

    public Result(String code, boolean result, String msg, Object data) {
        this.code = code;
        this.result = result;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 业务失败构建方法，不带错误信息的
     */
    public static Result failed(){

        return new Result(FAILED,false);
    }
    /**
     * 业务失败构建方法，带错误信息的
     */
    public static Result failed(String msg){

        return new Result(FAILED,false,msg);
    }
    /**
     * 业务失败构建方法，带错误码和错误信息的
     */
    public static Result failed(String code,String msg){

        return new Result(code, false, msg);
    }
    /**
     * 业务成功构建方法，不带提示信息的
     */
    public static Result success(){

        return new Result();
    }
    /**
     * 业务成功构建方法，带提示信息的
     */
    public static Result success(String msg){

        return new Result(SUCCESS,true,msg);
    }

    /**
     * 业务成功构建方法，带数据的
     */
    public static Result success(Object data){

        return new Result(SUCCESS,true,null,data);
    }

    /**
     * 业务成功构建方法，带消息和数据的
     */
    public static Result success(String msg,Object data){

        return new Result(SUCCESS,true,msg,data);
    }
}